package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonSmsMapper;
import com.yungu.swift.common.model.dto.CommonIdentifyDto;
import com.yungu.swift.common.service.CommonSmsService;
import com.yungu.swift.common.util.DefaultMessageSend;
import com.yungu.swift.common.util.SendSmsUtil;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Date;


/**
 * Copyright © 厦门云谷互联科技有限公司 2018-2020 All Right Reserved
 * <p>
 * 验证码业务逻辑处理接口实现类
 *
 * @author qfHan - Generate
 * @version 1.0.0-RELEASE
 * @since 2018-07-03 15:24
 */
@Slf4j
@Service
@ConfigurationProperties(prefix = "sms.common")
public class CommonSmsServiceImpl extends BaseServiceImpl<CommonIdentifyDto> implements CommonSmsService {

    @Autowired
    private CommonSmsMapper commonIdentifyMapper;

    @Override
    public IMapper<CommonIdentifyDto> getMapper() {
        return commonIdentifyMapper;
    }


    private Boolean send = true;

    /**
     * 发送验证码
     *
     * @param mobile
     * @param appid
     * @return
     */
    @Override
    public ResponseData<Boolean> sendIdentify(String mobile, String appid) {
        log.info("--------开始短信发送,不是进入阿里的-------");
        //过滤验证码
        String left = StringUtils.left(mobile, 3);
        if (MagicConstant.STRING_ONE_FOUR_TWO.equals(left)) {
            return ResponseData.buildSuccessResponse("发送成功", Boolean.TRUE);
        }
        CommonIdentifyDto identify = new CommonIdentifyDto();
        //生成4位数code
        String identifyCode = String.format("%04d", Double.valueOf(Math.random() * 10000).intValue());
        identify.setIdentifyCode(identifyCode);
        identify.setUuid(StringUtils.buildUUID());
        identify.setStatus(MagicConstant.INT_ONE);
        identify.setMobile(mobile);
        identify.setAppid(appid);
        identify.setCreateOn(new Date());
        //保存验证码验证信息
        commonIdentifyMapper.add(identify);
        //发送验证码并返回结果
        Boolean smsSend = DefaultMessageSend.smsSend(mobile, "您的验证码：" + identifyCode, appid);
        return ResponseData.buildSuccessResponse(smsSend);
    }

    /**
     * 发送自定义短信
     *
     * @param mobile
     * @param message
     * @param appid
     * @return
     */
    @Override
    public ResponseData<Boolean> sendCommon(String mobile, String message, String appid) {
        if (mobile.startsWith(MagicConstant.STRING_ONE_FOUR_TWO)) {
            return ResponseData.buildSuccessResponse("发送成功", Boolean.TRUE);
        }
        // 短信开关
        if (send) {
            log.info("短信发送开关关闭");
            return ResponseData.buildSuccessResponse("发送成功", Boolean.TRUE);
        }
        log.info("--------开始短信发送-------");
        Boolean smsSend = DefaultMessageSend.smsSend(mobile, message, appid);
        return ResponseData.buildSuccessResponse(smsSend);
    }

    /**
     * 发送验证码
     *
     * @param mobile
     * @param appid
     * @return
     */
    @Override
    public ResponseData<Boolean> sendAliIdentify(String mobile, String appid) {
        log.info("--------开始短信发送,进入阿里的-------");
        //过滤验证码
        String left = StringUtils.left(mobile, 3);
        if (MagicConstant.STRING_ONE_FOUR_TWO.equals(left)) {
            return ResponseData.buildSuccessResponse("发送成功", Boolean.TRUE);
        }
        CommonIdentifyDto identify = new CommonIdentifyDto();
        //生成4位数code
        String identifyCode = String.format("%04d", Double.valueOf(Math.random() * 10000).intValue());
        identify.setIdentifyCode(identifyCode);
        identify.setUuid(StringUtils.buildUUID());
        identify.setStatus(MagicConstant.INT_ONE);
        identify.setMobile(mobile);
        identify.setAppid(appid);
        identify.setCreateOn(new Date());
        //保存验证码验证信息
        commonIdentifyMapper.add(identify);
        //发送验证码并返回结果
        Boolean smsSend = SendSmsUtil.smsSend(mobile, identifyCode,"0");
        return ResponseData.buildSuccessResponse(smsSend);
    }

    /**
     * 发送自定义短信
     *
     * @param mobile
     * @param message
     * @param appid
     * @return
     */
    @Override
    public ResponseData<Boolean> sendAliCommon(String mobile, String message, String appid,String type) {
        log.info("到发送短信的方法了");
        if (mobile.startsWith(MagicConstant.STRING_ONE_FOUR_TWO)) {
            return ResponseData.buildSuccessResponse("发送成功", Boolean.TRUE);
        }
        log.info("--------开始短信发送-------");
        Boolean smsSend = SendSmsUtil.smsSend(mobile, message, type);
        return ResponseData.buildSuccessResponse(smsSend);
    }

    public void setSend(Boolean send) {
        this.send = send;
    }
}
